<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
 
if (isset($_COOKIE[USER])) {
  $_SESSION[USER] = json_decode($_COOKIE[USER]);
}


$path = MiscUtils::getParam('p', 'pagelogin') . '\\';
$template = MiscUtils::getParam('t', 'pageuserlogin');
$options = MiscUtils::getParam('opt', json_encode(array()));
$menu1 = MiscUtils::getParam('m1', '');
$menu2 = MiscUtils::getParam('m2', '');
$menu3 = MiscUtils::getParam('m3', '');

$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[USER]);
  setcookie(USER, '', time() - 1, PATH, DOMAIN);
  $smarty->assign('path', 'pagelogin\\');
  $smarty->assign('template', 'pageuserlogin');
  $smarty->display("pagelogin\\base.tpl");
  return; /* SignOut */
}

if (isset($_SESSION[USER])) {
  if ($_SESSION[USER]->type == USER_TYPE_MANAGER_ASSISTANT) {
    $path = 'pageasst\\';
    $template = ($template == 'pageuserlogin') ? 'pageb1assignwork' : $template;
  } else if ($_SESSION[USER]->type == USER_TYPE_CUSTOMER_SERVICE) {
    $path = 'pageserv\\';
    $template = ($template == 'pageuserlogin') ? 'pageb1choosedesigner' : $template;
  }
  
  if ($menu1 == '') {
    if ($template == 'managetasks') {
      $menu1 = 'a1';
      $menu2 = 'b1';
      $menu3 = 'c1';
    } else if ($template == 'managecustomers') {
      $menu1 = 'a2';
      $menu2 = 'b1';
    } else if ($template == 'managestats') {
      $menu1 = 'a3';
      $menu2 = 'b1';
    } else if ($template == 'manageusers') {
      $menu1 = 'a4';
      $menu2 = 'b1';
    } else {
      $menu1 = 'a1';
      $menu2 = 'b1';
      $menu3 = 'c1';
    }
  }
  
  if ($template == 'managetasks') {
    $template = 'pageb1assignwork';
  } else if ($template == 'managecustomers') {
    $template = 'pagecustomers';
  } else if ($template == 'manageusers') {
    $template = 'pagebackendusers';
  }
  
  $smarty->assign('path', $path);
  $smarty->assign('template', $template);

  $smarty->assign('menu1', $menu1);
  $smarty->assign('menu2', $menu2);
  $smarty->assign('menu3', $menu3);
  
  $smarty->assign('user', json_encode($_SESSION[USER]));
  $smarty->assign('sd', json_encode(SimpleDate::create()));
  $smarty->assign('options', urldecode($options));
  $smarty->display($path . 'base.tpl');
  return;
} else {
  $smarty->assign('path', 'pagelogin\\');
  $smarty->assign('template', 'pageuserlogin');
  $smarty->display("pagelogin\\base.tpl");
  return; /* SignIn Required */
}
?>